/*
 * BACmdHead.java
 * classes : com.qim.basdk.cmd.data.BACmdHead
 * @author xuwei
 * V 1.0.0
 * Created at 2015年8月20日 上午11:32:34
 */
package com.langsheng.lsintell.cmd.data;

import com.langsheng.lsintell.utils.LSConst;

/**
 * com.qim.basdk.cmd.data.LSCmdHead
 * <p>
 * 数据包报头
 * <p>
 * 报头数据长度固定16个字节
 * <p>
 * -----------------------------------------------------
 * <p>
 * |00000000 - 0000000f - 10 - 01 - 0000 - 00 - 00 - 00 |
 * <p>
 * | 包长度 - 序号 - 包头长度 - 协议版本 - 命令 - 状态码 - 加密方式 - 预留 |
 * <p>
 * ---------------------------------------------------
 *
 * @author xuwei
 *         <p>
 *         created at 2015年8月20日 上午11:32:34
 */
public class LSCmdHead {
    public final static String TAG = "LSCmdHead";

    public final static int ENCRYPT_TYPE_AES256 = 1;
    public final static int ENCRYPT_TYPE_SM4 = 2;

    /**
     * 报长度 四个字节
     */
    private int cmdLength = 0x00;
    /**
     * 序号 四个字节
     */
    private int cmdOrder = 0x00;
    /**
     * 包头长度 一个字节
     */
    private byte encryptKey1 = 0x00;
    /**
     * 协议版本 一个字节
     */
    private byte encryptKey2 = 0x00;
    /**
     * 指令名称 两个字节
     */
    private short cmdCode = 0x00;
    /**
     * 状态码 一个字节
     */
    private byte cmdStatus = 0x00;
    /**
     * 加密方式 一个字节
     */
    private byte encryptType = 0x00;
    /**
     * 预留 两个字节
     */
    private byte reserve1 = 0x00;
    private byte reserve2 = 0x00;

    /**
     * 将报头数据打包
     *
     * @return
     */
    public LSDataBuffer getCmdHead() {
        LSDataBuffer ret = new LSDataBuffer();
        ret.write(cmdLength);
        ret.write(cmdOrder);
        ret.write(encryptKey1);
        ret.write(encryptKey2);
        ret.write(cmdCode);
        ret.write(cmdStatus);
        ret.write(encryptType);
        ret.write(reserve1);
        ret.write(reserve2);
        return ret;
    }

    /**
     * 从缓存中读取报头解析
     *
     * @param buffer
     */
    public void parseHead(LSDataBuffer buffer) {
        cmdLength = buffer.readInt();
        cmdOrder = buffer.readInt();
        encryptKey1 = buffer.readByte();
        encryptKey2 = buffer.readByte();
        cmdCode = buffer.readShort();
        cmdStatus = buffer.readByte();
        encryptType = buffer.readByte();
        reserve1 = buffer.readByte();
        reserve2 = buffer.readByte();
    }

    /**
     * @return the cmdLength
     */
    public int getCmdLength() {
        return this.cmdLength;
    }

    /**
     * @param cmdLength the cmdLength to set
     */
    public void setCmdLength(int cmdLength) {
        this.cmdLength = cmdLength;
    }

    /**
     * @return the cmdOrder
     */
    public int getCmdOrder() {
        return this.cmdOrder;
    }

    /**
     * @param cmdOrder the cmdOrder to set
     */
    public void setCmdOrder(int cmdOrder) {
        this.cmdOrder = cmdOrder;
    }

    /**
     * @return the encryptKey1
     */
    public byte getEncryptKey1() {
        return this.encryptKey1;
    }

    /**
     * @param encryptKey1 the encryptKey1 to set
     */
    public void setEncryptKey1(byte encryptKey1) {
        this.encryptKey1 = encryptKey1;
    }

    /**
     * @return the encryptKey2
     */
    public byte getEncryptKey2() {
        return this.encryptKey2;
    }

    /**
     * @param encryptKey2 the encryptKey2 to set
     */
    public void setEncryptKey2(byte encryptKey2) {
        this.encryptKey2 = encryptKey2;
    }

    /**
     * @return the cmdName
     */
    public short getCmdCode() {
        return this.cmdCode;
    }

    /**
     * @param cmdCode the cmdName to set
     */
    public void setCmdCode(short cmdCode) {
        this.cmdCode = cmdCode;
    }

    /**
     * @return the cmdStatus
     */
    public byte getCmdStatus() {
        return this.cmdStatus;
    }

    /**
     * @param cmdStatus the cmdStatus to set
     */
    public void setCmdStatus(byte cmdStatus) {
        this.cmdStatus = cmdStatus;
    }

    /**
     * @return the encryptType
     */
    public byte getEncryptType() {
        return this.encryptType;
    }

    /**
     * @param encryptType the encryptType to set
     */
    public void setEncryptType(byte encryptType) {
        this.encryptType = encryptType;
    }

    /**
     * @return the reserve1
     */
    public byte getReserve1() {
        return this.reserve1;
    }

    /**
     * @param reserve1 the reserve1 to set
     */
    public void setReserve1(byte reserve1) {
        this.reserve1 = reserve1;
    }

    /**
     * @return the reserve2
     */
    public byte getReserve2() {
        return this.reserve2;
    }

    /**
     * @param reserve2 the reserve2 to set
     */
    public void setReserve2(byte reserve2) {
        this.reserve2 = reserve2;
    }

    @Override
    public String toString() {
        StringBuilder ret = new StringBuilder();
        ret.append(cmdCode);
        ret.append(LSConst.CMD_SPLIT_LINE_EX);
        ret.append(cmdLength);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(cmdOrder);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(encryptKey1);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(encryptKey2);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append("0x" + Long.toHexString(cmdCode));
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(cmdStatus);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(encryptType);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(reserve1);
        ret.append(LSConst.CMD_SPLIT_PARAM);
        ret.append(reserve2);
        return ret.toString();
    }
}
